function setModalSize(){
	var documentW = $(window).width();
	var documentH = $(window).height();
	$('#modalW').width(documentW * 0.6);
	$('#modalH').height(documentH * 0.7);	
}

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("flow");  //Set current page
	App.init(); //Initialise plugins and elements
	

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增字典
	$('#add').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("流程新增");
		$("#modal").modal('show');
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDeclear").on("hidden.bs.modal", function() {
      	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'flow/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	flownumber: $('#flownumber').val(),
                	flowname: $('#flowname').val()
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'flownumber',
	        title: '流程编号'
	    }, {
	        field: 'flowname',
	        title: '流程名称'
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
            '<button type="button" class="NextNode btn btn-inverse  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-link"></span>节点配置</button>',
//            '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
            '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
    ].join('');
}


window.operateEvents = {
    'click .NextNode': function (e, value, row, index) {		//列表操作栏【节点配置】
    	setModalSize();

		var flownumber = row.flownumber;
    	
    	$('#ifModal').attr("src", "nextnode.html?flownumber=" + flownumber);
    	
		$('#modalTile').html("节点配置");
		$("#modal").modal('show');
    },    
    'click .Edit': function (e, value, row, index) {		//列表操作栏【编辑】
    	setModalSize();
    	
    	var itemno = row.itemno;
    	
    	$('#ifModal').attr("src", "edit.html?itemno=" + itemno);
    	
		$('#modalTile').html("政务项目修改");
		$("#modal").modal('show');
    	
    },
    'click .Delete': function (e, value, row, index) {		//列表操作栏【删除】
		var flownumber = row.flownumber;
		window.wxc.xcConfirm('是否<strong>删除</strong>流程数据?<br /><span style="color:red;">['+ flownumber +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: projectPath + "flow/delete.do",
					data: {"flownumber": flownumber},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
    }
 };

function check(data){	
	var result = true;
	

	if($('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').length == 0){
		window.wxc.xcConfirm('请增加<span style="color:red;"><strong>字典明细</strong></span>的行数据！', window.wxc.xcConfirm.typeEnum.info);
		
		return false;
	}
	
	
	var mustStr = "dictcode,dictname";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
		
	
	if(strMsg != ""){
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.info);
	}
	
	return result;
}